package com.weimob.mcloud.wechatwork.customer.convertor.tag;

import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.request.tag.CorpTagAdapterDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.request.tag.CorpTagGroupAdapterDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.CorpTagInfo;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.Tag;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagGroup;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.tag.TagBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.tag.TagGroupBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.tag.TagDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.tag.TagGroupDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.tag.v2.*;
import com.weimob.mcloud.wechatwork.customer.service.user.module.req.tag.TagAddDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/11/16 10:21 下午
 */
public class TagConvert {

    public static List<TagBO> toTagBOS(List<Tag> tags) {

        if (CollectionUtils.isEmpty(tags)) {
            return new ArrayList<>();
        }

        List<TagBO> tagBOList = new ArrayList<>();
        for (Tag tag : tags) {
            TagBO tagBO = toTagBO(tag);
            tagBOList.add(tagBO);
        }

        return tagBOList;
    }

    public static TagBO toTagBO(Tag tag) {
        TagBO tagBO = new TagBO();
        tagBO.setCdpTagId(tag.getCdpTagId());
        tagBO.setExternalTagId(tag.getExternalTagId());
        tagBO.setTagName(tag.getTagName());
        tagBO.setTagSource(tag.getTagSource());

        TagGroup tagGroup = tag.getTagGroup();
        if (Objects.nonNull(tagGroup)) {
            TagGroupBO tagGroupBO = new TagGroupBO();
            tagGroupBO.setExternalTagGroupId(tagGroup.getExternalTagGroupId());
            tagGroupBO.setTagGroupName(tagGroup.getTagGroupName());
            tagGroupBO.setTagSource(tagGroup.getTagSource());
            tagGroupBO.setCdpTagGroupId(tagGroup.getCdpTagGroupId());
            tagBO.setTagGroupBO(tagGroupBO);

        }

        return tagBO;
    }

    public static List<String> toExternalTagIds(List<TagBO> tagBOList) {
        if (CollectionUtils.isEmpty(tagBOList)) {
            return new ArrayList<>();
        }

        List<String> externalTagIds = new ArrayList<>();
        for (TagBO tagBO : tagBOList) {
            externalTagIds.add(tagBO.getExternalTagId());
        }


        return externalTagIds;
    }

    public static List<String> toCdpTagIds(List<TagBO> tagBOList) {
        if (CollectionUtils.isEmpty(tagBOList)) {
            return new ArrayList<>();
        }

        List<String> cdpTagIds = new ArrayList<>();
        for (TagBO tagBO : tagBOList) {
            cdpTagIds.add(tagBO.getCdpTagId());
        }


        return cdpTagIds;
    }

    public static Map<String, Set<String>> toCdpTagGroupIdMap(List<TagDTO> tagDTOS) {
        if (CollectionUtils.isEmpty(tagDTOS)) {
            return new HashMap<>();
        }

        Map<String, Set<String>> map = new HashMap<>();
        for (TagDTO tagDTO : tagDTOS) {
            TagGroupDTO tagGroupDTO = tagDTO.getTagGroupDTO();
            if (Objects.isNull(tagGroupDTO)) {
                continue;
            }

            Set<String> cdpTagIds = map.get(tagGroupDTO.getCdpTagGroupId());
            if (CollectionUtils.isEmpty(cdpTagIds)) {
                cdpTagIds = new HashSet<>();
                cdpTagIds.add(tagDTO.getCdpTagId());
                map.put(tagGroupDTO.getCdpTagGroupId(), cdpTagIds);
            } else {
                cdpTagIds.add(tagDTO.getCdpTagId());
            }

        }

        return map;
    }

    public static Map<String, TagBO> toCdpTagIdAndTagBO(List<TagGroupBO> tagGroupBOS) {
        if (CollectionUtils.isEmpty(tagGroupBOS)) {
            return new HashMap<>();
        }

        Map<String, TagBO> cdpTagIdAndCdpTagGroupIdMap = new HashMap<>();
        for (TagGroupBO tagGroupBO : tagGroupBOS) {
            List<TagBO> tagBOList = tagGroupBO.getTagBOList();
            if (CollectionUtils.isEmpty(tagBOList)) {
                continue;
            }


            for (TagBO tagBO : tagBOList) {

                TagGroupBO tagGroup = new TagGroupBO();
                tagGroup.setCdpTagGroupId(tagGroupBO.getCdpTagGroupId());
                tagGroup.setExternalTagGroupId(tagGroupBO.getExternalTagGroupId());
                tagGroup.setTagGroupName(tagGroupBO.getTagGroupName());
                tagGroup.setTagType(tagGroupBO.getTagType());
                tagGroup.setTagSource(tagGroupBO.getTagSource());
                tagBO.setTagGroupBO(tagGroup);

                cdpTagIdAndCdpTagGroupIdMap.put(tagBO.getCdpTagId(), tagBO);
            }
        }


        return cdpTagIdAndCdpTagGroupIdMap;
    }

    public static TagDTO toTagDTO(Tag tag) {
        if (Objects.isNull(tag)) {
            return null;
        }

        TagDTO tagDTO = new TagDTO();
        tagDTO.setExternalTagId(tag.getExternalTagId());
        tagDTO.setCdpTagId(tag.getCdpTagId());
        tagDTO.setDeleted(tag.getDeleted());
        tagDTO.setTagSource(tag.getTagSource());
        tagDTO.setTagName(tag.getTagName());

        TagGroup tagGroup = tag.getTagGroup();
        if (Objects.nonNull(tagGroup)) {
            TagGroupDTO tagGroupDTO = new TagGroupDTO();
            tagGroupDTO.setExternalTagGroupId(tagGroup.getExternalTagGroupId());
            tagGroupDTO.setCdpTagGroupId(tagGroup.getCdpTagGroupId());
            tagGroupDTO.setTagGroupName(tagGroup.getTagGroupName());
            tagGroupDTO.setTagSource(tagGroup.getTagSource());
            tagGroupDTO.setDeleted(tagGroup.getDeleted());
            tagDTO.setTagGroupDTO(tagGroupDTO);
        }

        return tagDTO;
    }

    public static Map<String, Set<String>> toCdpTagGroupIdAndCdpTagIdsMap(List<TagAddDTO> tagAddDTOS) {
        if (CollectionUtils.isEmpty(tagAddDTOS)) {
            return new HashMap<>();
        }

        Map<String, Set<String>> map = new HashMap<>();
        for (TagAddDTO tagAddDTO : tagAddDTOS) {
            String cdpTagGroupId = tagAddDTO.getCdpTagGroupId();
            if (Objects.isNull(cdpTagGroupId)) {
                continue;
            }

            Set<String> cdpTagIds = map.get(cdpTagGroupId);
            if (CollectionUtils.isEmpty(cdpTagIds)) {
                cdpTagIds = new HashSet<>();
                cdpTagIds.add(tagAddDTO.getCdpTagId());
                map.put(cdpTagGroupId, cdpTagIds);
            } else {
                cdpTagIds.add(tagAddDTO.getCdpTagId());
            }

        }

        return map;
    }

    public static List<TagBO> filterNameForNull(List<TagBO> tagBOList) {

        if (CollectionUtils.isEmpty(tagBOList)) {
            return new ArrayList<>();
        }

        List<TagBO> afterFilter = new ArrayList<>();
        for (TagBO tagBO : tagBOList) {
            if (Objects.isNull(tagBO) || StringUtils.isBlank(tagBO.getTagName())) {
                continue;
            }

            TagGroupBO tagGroupBO = tagBO.getTagGroupBO();
            if (Objects.isNull(tagGroupBO) || StringUtils.isBlank(tagGroupBO.getTagGroupName())) {
                continue;
            }
            afterFilter.add(tagBO);
        }

        return afterFilter;
    }

    public static List<TagDTO> filterTagNameForNull(List<TagDTO> tagDTOList) {

        if (CollectionUtils.isEmpty(tagDTOList)) {
            return new ArrayList<>();
        }

        List<TagDTO> afterFilter = new ArrayList<>();
        for (TagDTO tagDTO : tagDTOList) {
            if (Objects.isNull(tagDTO) || StringUtils.isBlank(tagDTO.getTagName())) {
                continue;
            }

            TagGroupDTO tagGroupDTO = tagDTO.getTagGroupDTO();
            if (Objects.isNull(tagGroupDTO) || StringUtils.isBlank(tagGroupDTO.getTagGroupName())) {
                continue;
            }
            afterFilter.add(tagDTO);
        }

        return afterFilter;
    }

    public static CorpTagGroupAdapterDTO toCorpTagGroupAdapterDTO(CorpTagGroupDTO corpTagGroupDTO) {
        CorpTagGroupAdapterDTO corpTagGroupAdapterDTO = new CorpTagGroupAdapterDTO();
        corpTagGroupAdapterDTO.setGroupId(corpTagGroupDTO.getCorpTagGroupId());
        corpTagGroupAdapterDTO.setGroupName(corpTagGroupDTO.getTagGroupName());

        List<CorpTagDTO> tagList = corpTagGroupDTO.getTagList();


        List<CorpTagAdapterDTO> corpTagAdapterDTOList = new ArrayList<>();
        for (CorpTagDTO corpTagDTO : tagList) {
            CorpTagAdapterDTO corpTagAdapterDTO = new CorpTagAdapterDTO();
            corpTagAdapterDTO.setTagId(corpTagDTO.getCorpTagId());
            corpTagAdapterDTO.setTagName(corpTagDTO.getTagName());

            corpTagAdapterDTOList.add(corpTagAdapterDTO);
        }

        corpTagGroupAdapterDTO.setTagList(corpTagAdapterDTOList);


        return corpTagGroupAdapterDTO;

    }

    public static CorpTagGroupDTO toCorpTagGroupDTO(CorpTagGroupAdapterDTO corpTagGroupAdapterDTO) {
        CorpTagGroupDTO corpTagGroupDTO = new CorpTagGroupDTO();
        corpTagGroupDTO.setTagGroupName(corpTagGroupAdapterDTO.getGroupName());
        corpTagGroupDTO.setCorpTagGroupId(corpTagGroupAdapterDTO.getGroupId());

        List<CorpTagAdapterDTO> tagAdapterList = corpTagGroupAdapterDTO.getTagList();
        if (CollectionUtils.isEmpty(tagAdapterList)) {
            return corpTagGroupDTO;
        }

        List<CorpTagDTO> tagList = new ArrayList<>();
        for (CorpTagAdapterDTO tagAdapterDTO : tagAdapterList) {
            CorpTagDTO corpTagDTO = new CorpTagDTO();
            corpTagDTO.setCorpTagId(tagAdapterDTO.getTagId());
            corpTagDTO.setTagName(tagAdapterDTO.getTagName());

            tagList.add(corpTagDTO);
        }

        corpTagGroupDTO.setTagList(tagList);

        return corpTagGroupDTO;
    }

    public static List<String> toCorpTagIdList(List<CorpTagDTO> corpTagDTOS) {
        if (CollectionUtils.isEmpty(corpTagDTOS)) {
            return new ArrayList<>();
        }
        Set<String> corpTagIdSet = new HashSet<>();
        for (CorpTagDTO corpTagDTO : corpTagDTOS) {
            if (Objects.isNull(corpTagDTO) || StringUtils.isBlank(corpTagDTO.getCorpTagId())) {
                continue;
            }
            corpTagIdSet.add(corpTagDTO.getCorpTagId());
        }

        if (CollectionUtils.isEmpty(corpTagIdSet)) {
            return new ArrayList<>();
        }

        return Lists.newArrayList(corpTagIdSet);
    }

    public static Set<String> toCorpTagIds(List<CorpTagInfo> corpTagInfoList) {
        if (CollectionUtils.isEmpty(corpTagInfoList)) {
            return new HashSet<>();
        }

        Set<String> corpTagIds = new HashSet<>();
        for (CorpTagInfo corpTagInfo : corpTagInfoList) {
            corpTagIds.add(corpTagInfo.getTagId());
        }


        return corpTagIds;
    }

    public static Map<String, String> toCorpTagIdAndCorpGroupIdMap(List<Tag> tagList) {
        if (CollectionUtils.isEmpty(tagList)) {
            return new HashMap<>();
        }

        Map<String, String> corpTagIdAndCorpGroupIdMap = new HashMap<>();
        for (Tag tag : tagList) {
            TagGroup tagGroup = tag.getTagGroup();
            if (Objects.isNull(tagGroup)) {
                continue;
            }
            corpTagIdAndCorpGroupIdMap.put(tag.getExternalTagId(), tagGroup.getExternalTagGroupId());
        }


        return corpTagIdAndCorpGroupIdMap;
    }

    public static Set<String> toCorpTagId(CustomerCorpTagDTO customerCorpTagDTO) {
        if (Objects.isNull(customerCorpTagDTO)) {
            return new HashSet<>();
        }

       Set<String> corpTagId = new HashSet<>();
        if (CollectionUtils.isNotEmpty(customerCorpTagDTO.getAddTagList())) {
            for (CorpTagDTO corpTagDTO : customerCorpTagDTO.getAddTagList()) {
                if (Objects.isNull(corpTagDTO) || StringUtils.isBlank(corpTagDTO.getCorpTagId())) {
                    continue;
                }
                corpTagId.add(corpTagDTO.getCorpTagId());
            }
        }

        if (CollectionUtils.isNotEmpty(customerCorpTagDTO.getRemoveTagList())) {
            for (CorpTagDTO corpTagDTO : customerCorpTagDTO.getRemoveTagList()) {
                if (Objects.isNull(corpTagDTO) || StringUtils.isBlank(corpTagDTO.getCorpTagId())) {
                    continue;
                }
                corpTagId.add(corpTagDTO.getCorpTagId());
            }
        }

        return corpTagId;
    }

    public static Map<String, String> toTagIdAndTagNameMap(List<Tag> tags) {
        if (CollectionUtils.isEmpty(tags)) {
            return new HashMap<>();
        }

        Map<String, String> tagIdAndNameMap = new HashMap<>();
        for (Tag tag: tags) {
            tagIdAndNameMap.put(tag.getExternalTagId(), tag.getTagName());
        }

        return tagIdAndNameMap;
    }

    public static List<TagDTO> toTagDTOList(List<CdpTagGroupItemDTO> tagLis) {
        if (CollectionUtils.isEmpty(tagLis)) {
            return new ArrayList<>();
        }

        List<TagDTO> tagDTOList = new ArrayList<>();
        for (CdpTagGroupItemDTO cdpTagGroupDTO : tagLis) {
            if (CollectionUtils.isEmpty(cdpTagGroupDTO.getTagList())) {
                continue;
            }

            for (CdpTagItemDTO cdpTagDTO : cdpTagGroupDTO.getTagList()) {
                TagDTO tagDTO = new TagDTO();
                tagDTO.setCdpTagId(cdpTagDTO.getCdpTagId());
                tagDTO.setTagName(cdpTagDTO.getTagName());

                TagGroupDTO tagGroupDTO = new TagGroupDTO();
                tagGroupDTO.setCdpTagGroupId(cdpTagGroupDTO.getCdpTagGroupId());
                tagGroupDTO.setTagGroupName(cdpTagGroupDTO.getTagGroupName());
                tagDTO.setTagGroupDTO(tagGroupDTO);


                tagDTOList.add(tagDTO);
            }
        }


        return tagDTOList;
    }

    public static Map<String, Set<String>> toCdpGroupIdAndCdpTagIdsMap(List<CdpTagGroupItemDTO> tagGroupList) {
        if (CollectionUtils.isEmpty(tagGroupList)) {
            return new HashMap<>();
        }

        Map<String, Set<String>> map = new HashMap<>();
        for (CdpTagGroupItemDTO tagGroupItemDTO : tagGroupList) {
            String cdpTagGroupId = tagGroupItemDTO.getCdpTagGroupId();
            if (Objects.isNull(cdpTagGroupId)) {
                continue;
            }


            Set<String> cdpTagIds = map.get(cdpTagGroupId);
            for (CdpTagItemDTO cdpTagItemDTO : tagGroupItemDTO.getTagList()) {

                if (CollectionUtils.isEmpty(cdpTagIds)) {
                    cdpTagIds = new HashSet<>();
                    cdpTagIds.add(cdpTagItemDTO.getCdpTagId());
                    map.put(cdpTagGroupId, cdpTagIds);
                } else {
                    cdpTagIds.add(cdpTagItemDTO.getCdpTagId());
                }
            }
        }

        return map;
    }

    public static List<String> toCorpTagIdsFromCorpTagDTO(List<CorpTagDTO> corpTagDTOList) {
        if (CollectionUtils.isEmpty(corpTagDTOList)) {
            return new ArrayList<>();
        }

        List<String> corpTagIds = new ArrayList<>();
        for (CorpTagDTO corpTagDTO : corpTagDTOList) {
            corpTagIds.add(corpTagDTO.getCorpTagId());
        }


        return corpTagIds;
    }

    public static Set<String> toTagIds(List<Tag> tags) {
        if (CollectionUtils.isEmpty(tags)) {
            return new HashSet<>();
        }

        Set<String> tagIds = new HashSet<>();
        for (Tag tag : tags) {
            tagIds.add(tag.getExternalTagId());
        }


        return tagIds;
    }
}
